<?php
/**
 *description:Auto
 *author:李飞
 *time:2016/12/29
 */

namespace Admin\Controller;
use Think\Controller;
class AutoTestController extends Controller {
	public function base_config(){
		//加载自定义配置信息
		$m=M('Config',C('DB_PRS'),C('DB_DEPOT'));
		$list=$m->where(array('status'=>1))->getField('en_name,value');
		C('base_config',$list);
	}
	//批量出库申报接口（更新申报信息）
	public function get_methodbatch($times=1){
		C("DB_DEPOT.DB_TYPE","mysql");
		C("DB_DEPOT.DB_HOST","localhost");
		// C("DB_DEPOT.DB_NAME","hcf");
		C("DB_DEPOT.DB_NAME","tzwms_18020170001");
		C("DB_DEPOT.DB_USER","root");
		// C("DB_DEPOT.DB_PWD","root");
		C("DB_DEPOT.DB_PWD","8212dd1c4e");//250服务器
		// C("DB_DEPOT.DB_PWD","3fcb0c61d3");//136服务器
		C("DB_DEPOT.DB_PORT","3306");
		C("DB_PRS","tz_");
		C("DB_CHARSET","utf8");
		
		$p=M('Pushorder',C('DB_PRS'),C('DB_DEPOT'));
		$pa=M('Packing',C('DB_PRS'),C('DB_DEPOT'));
		$g=M('Pushordergoods',C('DB_PRS'),C('DB_DEPOT'));
		
		$map=array();
		$map['push_outboundstatus']=0;
		$map['is_delivery']=1;
		$map['push_outbounttimes']=0;
		
		$list=$p->alias('p')->field('id,logistics_no,order_no')->where($map)->limit(0,50)->order('id')->select();

		if(empty($list)){
			$times=$times+1;//二次申报效验
			
			$map=array();
			$map['push_outboundstatus']=0;
			$map['is_delivery']=1;
			$map['push_outboundmark']="";
		
			$list=$p->alias('p')->field('id,logistics_no,order_no')->where($map)->limit(0,1)->order('id')->select();
			if(empty($list)){
				die;
			}
		}
		$arr=array();
		$map_ids=array();
		foreach($list as $k=>$v){
			$arr[$k]['logistics_no']=$v['logistics_no'];
			$arr[$k]['pack_weight']=$pa->where(array('order_no'=>$v['order_no']))->getField('pack_weight');
			$goods_no=$g->where(array('order_id'=>$v['id']))->limit(1)->getField('goods_no');
			$arr[$k]['merchant_code']=substr($goods_no,0,9);
			$map_ids[]=$v['id'];
		}

		$this->base_config();
		$url=C('BASE_CONFIG.get_methodbatch');//批量更新出库信息
		// $url="http://debug.t-cang.com/test2018/oms/index.php/Api/WmsOut/batch_confirm_out";
		$map_s=array();
		$map_s['id']=array('in',$map_ids);
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		//post传值数据
		curl_setopt($ch, CURLOPT_POST, 1);
		//post的变量
		curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('data'=>$arr)));
		$output = curl_exec($ch);
		curl_close($ch);
		$res= json_decode($output,true);

		$data_out=array();
		$data_out['push_outbounttimes']=array('exp',"push_outbounttimes+1");
		if($res['flag']){//申报成功
			$data_out['push_outboundstatus']=1;
			$data_out['push_outboundtime']=time();
			$status=$p->alias('p')->where($map_s)->save($data_out);
			// echo $p->_sql();
			// if($status!==false){
				// $return['status']=1;
				// $return['message']=$res['tip'];
				// $this->ajaxReturn($return);
			// }else{
				// $data_out['push_outboundstatus']=0;
				// $status=$p->alias('p')->where($map_s)->save($data_out);//回滚操作
				// $return['status']=0;
				// $return['message']=$res['tip'];
				// $this->ajaxReturn($return);
			// }
		}else{//失败
			$data_out['push_outboundstatus']=0;
			if($times>1 and $times<=21){
				if(!$res['flag']){
					$data_out['push_outboundmark']=$res['tip'];
				}
			}
			$status=$p->alias('p')->where($map_s)->save($data_out);//回滚操作
			// $return['status']=0;
			// $return['message']=$res['tip'];
			// $this->ajaxReturn($return);
		}
		
		if($times>1 and $times<21){
			$this->get_methodbatch($times);
		}
    }
	//每日库存统计
	public function goods_log(){
		C("DB_DEPOT.DB_TYPE","mysql");
		C("DB_DEPOT.DB_HOST","localhost");
		// C("DB_DEPOT.DB_NAME","hcf");
		C("DB_DEPOT.DB_NAME","tzwms_18020170001");
		C("DB_DEPOT.DB_USER","root");
		// C("DB_DEPOT.DB_PWD","root");
		C("DB_DEPOT.DB_PWD","8212dd1c4e");//250服务器
		// C("DB_DEPOT.DB_PWD","3fcb0c61d3");//136服务器
		C("DB_DEPOT.DB_PORT","3306");
		C("DB_PRS","tz_");
		C("DB_CHARSET","utf8");

		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
		$cl=M('CheckstockLog',C('DB_PRS'),C('DB_DEPOT'));
        $m=M('Goods',C('DB_PRS'),C('DB_DEPOT'));
		$gl=M('GoodsLog',C('DB_PRS'),C('DB_DEPOT'));
		$bc=M('BooksCate',C('DB_PRS'),C('DB_DEPOT'));

		$p=M('Packing',C('DB_PRS'),C('DB_DEPOT'));
		$s=M('StorageOutbound',C('DB_PRS'),C('DB_DEPOT'));
		$ss=M('StatisticalStorage',C('DB_PRS'),C('DB_DEPOT'));
		
		$po=M('Pushorder',C('DB_PRS'),C('DB_DEPOT'));
		$pog=M('Pushordergoods',C('DB_PRS'),C('DB_DEPOT'));
		
		// $time=time()-3600*24*4;//两天前
		$time=time();//当前时间(查询时间)
		$time_today=date("Y-m-d",$time);//今天
		$time_nextday=date("Y-m-d",$time+3600*24);//明天
		
		$str_time_today=strtotime($time_today);//今天(时间戳)
		$str_time_nextday=strtotime($time_nextday);//明天(时间戳)
		//当日盘盈核增数量-begin
		$map_po=array();
		$map_po['_string']="examine_time>$str_time_today and examine_time<$str_time_nextday and cate=1";
		$goods_list_hz=$cl->where($map_po)->group('goods_no')->getField('goods_no,goods_name,sum(goods_nums) sum_goodsnums');
		//当日盘盈核增数量-end
		
		//当日盘亏核减数量-begin
		$map_po=array();
		$map_po['_string']="po.delivery_time>$str_time_today and po.delivery_time<$str_time_nextday and cate=2";
		$goods_list_hj=$cl->where($map_po)->group('goods_no')->getField('goods_no,goods_name,sum(goods_nums) sum_goodsnums');
		//当日盘亏核减数量-end
		
		//总盘盈核增数量-begin
		$map_po=array();
		$map_po['_string']="cate=1";
		$goods_list_hz_total=$cl->where($map_po)->group('goods_no')->getField('goods_no,goods_name,sum(goods_nums) sum_goodsnums');
		//总盘盈核增数量-end
		
		//总盘亏核减数量-begin
		$map_po=array();
		$map_po['_string']="cate=2";
		$goods_list_hj_total=$cl->where($map_po)->group('goods_no')->getField('goods_no,goods_name,sum(goods_nums) sum_goodsnums');
		//总盘亏核减数量-end

		//当日出库数量-begin
		$map_po=array();
		$map_po['_string']="po.delivery_time>$str_time_today and po.delivery_time<$str_time_nextday";
		$goods_list=$po->alias('po')->join('__PUSHORDERGOODS__ g on po.id=g.order_id')->where($map_po)->group('g.goods_no')->order('po.id')->getField('g.goods_no,g.goods_name,sum(g.goods_nums) sum_goodsnums');
		//当日出库数量-end
		
		//当日接收数量-begin
		$map_ps=array();
		$map_ps['_string']="po.add_time>$str_time_today and po.add_time<$str_time_nextday";
		$goods_list_ps=$po->alias('po')->join('__PUSHORDERGOODS__ g on po.id=g.order_id')->where($map_ps)->group('g.goods_no')->order('po.id')->getField('g.goods_no,g.goods_name,sum(g.goods_nums) sum_goodsnums');
		//当日接收数量-end
		// echo $po->_sql();
		// dump($goods_list_ps);
		// die;
		//总出库数量-begin
		$map_pack=array();
		$map_pack['is_delivery']=1;
		$goods_list_count_oubound_nums=$po->alias('po')->join('__PUSHORDERGOODS__ g on po.id=g.order_id')->where($map_pack)->group('g.goods_no')->order('po.id')->getField('g.goods_no,g.goods_name,sum(g.goods_nums) sum_goodsnums');
		//总出库数量-end
		
		//总接收数量-begin
		$map_pack=array();
		// $map_pack['is_delivery']=1;
		$goods_list_count_ps_nums=$po->alias('po')->join('__PUSHORDERGOODS__ g on po.id=g.order_id')->group('g.goods_no')->order('po.id')->getField('g.goods_no,g.goods_name,sum(g.goods_nums) sum_goodsnums');
		// echo $po->_sql();
		//总接收数量-end
		
		//剩余未出库数量-begin
		$map_pack=array();
		$map_pack['is_delivery']=0;
		$goods_list_count_nooubound_nums=$po->alias('po')->join('__PUSHORDERGOODS__ g on po.id=g.order_id')->where($map_pack)->group('g.goods_no')->order('po.id')->getField('g.goods_no,g.goods_name,sum(g.goods_nums) sum_goodsnums');
		//剩余未出库数量-end
		
		//总实收已上架数量-begin
		$map_pack=array();
		$map_pack['status']=1;
		$map_pack['inbound_status']=1;
		$map_pack['grounding_status']=1;
		$goods_list_grounding=$ao->where($map_pack)->group('goods_no')->order('id')->getField('goods_no,goods_name,sum(receive_amount) sum_goodsnums');
		//总实收已上架数量-end
		// echo $po->_sql();
		// die;
		//总实收未上架数量-begin
		$map_pack=array();
		$map_pack['status']=1;
		$map_pack['inbound_status']=1;
		$map_pack['grounding_status']=0;
		$goods_list_grounding_no=$ao->where($map_pack)->group('goods_no')->order('id')->getField('goods_no,goods_name,sum(receive_amount) sum_goodsnums');
		//总实收未上架数量-end
		
		//总实收已下架数量-begin
		$map_pack=array();
		$map_pack['status']=1;
		$map_pack['inbound_status']=1;
		$map_pack['grounding_status']=2;
		$goods_list_grounding_down=$ao->where($map_pack)->group('goods_no')->order('id')->getField('goods_no,goods_name,sum(receive_amount) sum_goodsnums');
		//总实收已下架数量-end
		//当日打包数量-begin
		$map_pack=array();
		$map_pack['_string']="DATE_FORMAT(p.created_at,'%Y-%m-%d')='$time_today'";
		$goods_list_pack=$p->alias('p')->join('__PUSHORDER__ po on p.order_no=po.order_no')->join('__PUSHORDERGOODS__ g on po.id=g.order_id')->where($map_pack)->group('g.goods_no')->order('po.id')->getField('g.goods_no,g.goods_name,sum(g.goods_nums) sum_goodsnums');
		//当日打包数量-end
		
		//当日称重数量-begin
		$map_weight=array();
		$map_weight['_string']="DATE_FORMAT(p.created_at,'%Y-%m-%d')='$time_today'";
		$map_weight['pack_weight']=array('exp','is not null');
		$goods_list_weight=$p->alias('p')->join('__PUSHORDER__ po on p.order_no=po.order_no')->join('__PUSHORDERGOODS__ g on po.id=g.order_id')->where($map_weight)->group('g.goods_no')->order('po.id')->getField('g.goods_no,g.goods_name,sum(g.goods_nums) sum_goodsnums');
		//当日称重数量-end
		
		//当日导单数量-begin
		// $url_inbound="http://erpvr.jm.com/index.php/Api/inbound_nums";
		// $url_inbound="http://wms.t-cang.net/erpts/index.php/Api/inbound_nums";//136服务器
		$url_inbound=C('ERP_URL')."/index.php/Api/inbound_nums";//250服务器
		$data_inbound=array();
		$data_inbound['time']=$time;
		$data_inbound['warehouse_code']='11330213';
		$data_inbound['manua_id']=$bc->where(array('status'=>1))->getField('code',true);
		$inbound_res=push($url_inbound,$data_inbound);
		$inbound_res=json_decode($inbound_res,true);
		$goods_list_inbound=$inbound_res['message'];
		//当日导单数量-end
		
		//总导单数量-begin
		$data_inbound=array();
		$data_inbound['time']=$time;
		$data_inbound['warehouse_code']='11330213';
		$data_inbound['total']=1;
		$del_res=push($url_inbound,$data_inbound);
		$del_res=json_decode($del_res,true);
		$goods_list_inbound_nums_total=$del_res['message'];
		//总导单数量-end
		
		//总删单数量-begin
		$data_inbound=array();
		$data_inbound['time']=$time;
		$data_inbound['warehouse_code']='11330213';
		$data_inbound['total']=1;
		$data_inbound['delete']=1;
		$del_res=push($url_inbound,$data_inbound);
		$del_res=json_decode($del_res,true);
		$goods_list_delete_nums_total=$del_res['message'];
		
		//总删单数量-end
		
		//当日删单数量-begin
		$data_inbound=array();
		$data_inbound['time']=$time;
		$data_inbound['warehouse_code']='11330213';
		$data_inbound['delete']=1;
		$del_res=push($url_inbound,$data_inbound);
		$del_res=json_decode($del_res,true);
		$goods_list_delete=$del_res['message'];
		//当日删单数量-end
		// dump($ao);
        //搜索条件
        // $map=array();
		// $map['m.status']=1;
		// $map['a.inbound_status']=1;
		// $map['a.grounding_status']=1;
		// $map['a.storage_amount']=array('GT',0);
		$info=$m->alias('m')->field('m.*,count(a.freez_amount) info_count,sum(a.freez_amount) freez_amount,sum(a.storage_amount) storage_amount')->join("__ARRIVAL_ORDER__ a on m.goods_no=a.goods_no and m.manua_id=a.manual_id","left")->group('m.goods_no,m.manua_id')->order("manua_id,goods_no")->select();
		$arr=array();
		//添加时间
		$now_time=time();
		foreach($info as $k=>$v){
			$arr[$k]['has_grounding']=$goods_list_grounding[$v['goods_no']]['sum_goodsnums'];//总实收已上架数量
			$arr[$k]['no_grounding']=$goods_list_grounding_no[$v['goods_no']]['sum_goodsnums'];//总实收未上架数量
			$arr[$k]['down_grounding']=$goods_list_grounding_down[$v['goods_no']]['sum_goodsnums'];//总实收已下架数量
			$arr[$k]['total_oubound_nums']=$goods_list_count_oubound_nums[$v['goods_no']]['sum_goodsnums'];//总出库数量
			$arr[$k]['total_oubound_nums_no']=$goods_list_count_nooubound_nums[$v['goods_no']]['sum_goodsnums'];//剩余未出库数量
			$arr[$k]['total_inbound_nums']=$goods_list_inbound_nums_total[$v['goods_no']]['sum_goodsnums'];//总导单数量
			$arr[$k]['total_receive_nums']=$goods_list_count_ps_nums[$v['goods_no']]['sum_goodsnums'];//总接收数量
			$arr[$k]['total_delete_nums']=$goods_list_delete_nums_total[$v['goods_no']]['sum_goodsnums'];//总删掉数量
			
			$arr[$k]['total_hz_nums']=$goods_list_hz_total[$v['goods_no']]['sum_goodsnums'];//总盘盈核增数量
			$arr[$k]['total_hj_nums']=$goods_list_hj_total[$v['goods_no']]['sum_goodsnums'];//总盘亏核减数量
			
			$arr[$k]['hz_nums']=$goods_list_hz[$v['goods_no']]['sum_goodsnums'];//当日盘盈核增数量
			$arr[$k]['hj_nums']=$goods_list_hj[$v['goods_no']]['sum_goodsnums'];//当日盘亏核减数量
			$arr[$k]['oubound_nums']=$goods_list[$v['goods_no']]['sum_goodsnums'];//当日出库数量
			$arr[$k]['receive_nums']=$goods_list_ps[$v['goods_no']]['sum_goodsnums'];//当日接收数量
			$arr[$k]['pack_nums']=$goods_list_pack[$v['goods_no']]['sum_goodsnums'];//当日打包数量
			$arr[$k]['weight_nums']=$goods_list_weight[$v['goods_no']]['sum_goodsnums'];//当日称重数量
			$arr[$k]['inbound_nums']=$goods_list_inbound[$v['goods_no']]['sum_goodsnums'];//当日导单数量
			$arr[$k]['delete_order_nums']=$goods_list_delete[$v['goods_no']]['sum_goodsnums'];//当日删单数量
			$arr[$k]['manua_id']=$v['manua_id'];
			$arr[$k]['goods_name']=$v['goods_name'];
			$arr[$k]['goods_no']=$v['goods_no'];
			$arr[$k]['storage_amount']=$v['storage_amount'];//剩余库存数量
			$arr[$k]['freez_nums']=$v['freez_amount'];//剩余冻结数量
			$arr[$k]['bar_code']=$v['bar_code'];
			$arr[$k]['add_time']=$now_time;
			$arr[$k]['status']=1;
		}
		// dump($arr);
		// dump(count($info));
		// die;
		//SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(now()),"%Y-%m-%d")
		$time=strtotime(date("Y-m-d",$now_time));
		$where_string="UNIX_TIMESTAMP(FROM_UNIXTIME(add_time,'%Y-%m-%d'))=$time";
		$count_gl=$gl->where($where_string)->count();
		if($count_gl==0){
			$res=$gl->addAll($arr);
			file_put_contents('./log/goods_log.log',date('Y-m-d H:i:s').var_export("---".$res,true).PHP_EOL,FILE_APPEND);
		}else{
			$gl->where($where_string)->delete();
			$res=$gl->addAll($arr);
			file_put_contents('./log/goods_log.log',date('Y-m-d H:i:s').var_export("---重复",true).PHP_EOL,FILE_APPEND);
		}
    }
}